package com.zxx.pattern.observerPattern.observer3;

import java.util.ArrayList;
import java.util.List;

/**
 * 主题具体实现类
 *
 * @author zxx on 2021/3/10
 */
public class WeatherData implements Subject3 {

    private List<Observer3> list;
    private float tempterature;
    private float pressure;
    private float humidity;

    public WeatherData() {
        this.list = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer3 observer) {
        list.add(observer);
    }

    @Override
    public void removeObserver(Observer3 observer) {
        int id = list.indexOf(observer);
        if (id >= 0) {
            list.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer3 observer : list) {
            observer.update(tempterature, humidity, pressure);
        }
    }

    //气象站得到更新的观测数据时，通知观察者
    public void measureChanged() {
        notifyObserver();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.tempterature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measureChanged();
    }

}
